home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 1066 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.9 KB

  1. Path: engnews1.Eng.Sun.COM!taumet!clamage
  2. From: jpotter@falcon.lhup.edu (John E. Potter)
  3. Newsgroups: comp.std.c++
  4. Subject: dynamic_cast
  5. Date: 12 Apr 1996 20:37:06 GMT
  6. Organization: East Stroudsburg University, Pennsylvania
  7. Approved: clamage@eng.sun.com (comp.std.c++)
  8. Message-ID: <4kmdqk$kjk@jake.esu.edu>
  9. NNTP-Posting-Host: taumet.eng.sun.com
  10. X-Nntp-Posting-Host: falcon.lhup.edu
  11. X-Newsreader: TIN [version 1.2 PL2]
  12. Content-Length: 2116
  13. X-Lines: 71
  14. Originator: clamage@taumet
  15.  
  16.  
  17. In trying to trace a perceived bug in g++2.7.2 to the DWP, I
  18. found that it seems vague.  I have paraphrased to save space.  If
  19. I messed it up, say so.  In the following, assume B is a polymorphic
  20. base of D and that A is unrelated.
  21.  
  22. 5.2.6 Dynamic cast [expr.dynamic.cast]
  23.  
  24. /1 dynamic_cast<T>(v)  T shall be a pointer or reference to a complete
  25. class type, or pointer to cv void.
  26.  
  27. -- clear
  28.  
  29. /2 v shall be a pointer or reference to a complete class type.  T and
  30. v must both be pointers or both be references.
  31.  
  32. -- clear
  33. dynamic_cast<A*>(0) is an error since int is not a pointer to complete
  34. class type.
  35. dynamic_cast<A*>(NULL) is an error since NULL ((void*)0) is not a pointer
  36. to complete class type.
  37. g++ flags them.
  38.  
  39. /3 "If the type of v is the same as the required result type (which,
  40. for convenience, will be called R ..., the result is v ..."
  41.  
  42. -- clear
  43. dynamic_cast<A*>(reinterpret_cast<A*>(0)) is ok and g++ accepts it.
  44. This is basically a noop.
  45.  
  46. /4 "If the value of v is a null pointer value in the pointer case, the
  47. result is the null pointer of type R."
  48.  
  49. -- not quite clear
  50. g++ interpreted the use of R here to mean that only case /3 null pointers
  51. were converted via a noop.  The remaining cases require non null pointers.
  52. It crashes on null pointers.
  53.  
  54. /5 If the T is to a base class of the v, the result is the unique base
  55. sub-object of the v.
  56.  
  57. -- hum
  58. The offset can be calculated at compile time and no run time check is
  59. needed.  What happens to null?
  60.  
  61. /6 Otherwise, v shall be to a polymorphic type.
  62.  
  63. -- hum
  64. shall be a pointer to, which could be null?
  65.  
  66. /7 If T is pointer to cv void, result is to complete object of v.
  67. Otherwise, run time check.
  68.  
  69. -- clear
  70. Compile time offset again.  Null?
  71.  
  72. /8 Runtime: If v is to public base of a T, result is to that T
  73.    else if the complete v has a base T, the result is that T
  74.    else fails
  75.  
  76. -- hum
  77. Nothing has said that T and v must be related.  It seems that
  78. dynamic_cast<A*>(someD*) is a valid statement which always fails.
  79.  
  80. /9 failed pointer is null, reference throws.
  81.  
  82. -- clear
  83. Example contains two C-style casts.  Should they be new style?
  84.  
  85. Food for thought,
  86. John
  87.  
  88.  
  89. [ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
  90. [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
  91. [ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
  92. [ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
  93. [ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]
  94.